/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package util.filter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author damfercer
 */
public class CollectionsFilter {
    public static <T> void filter(List<T> collection, Predicate<T> predicate) {
        if ((collection != null) && (predicate != null)) {
           Iterator<T> itr = collection.iterator();
              while(itr.hasNext()) {
                T obj = itr.next();
                if (!predicate.test(obj)) {
                   itr.remove();
                }
            }
        }
    }
    
   public static <T> List<T> filterOnNewCollection(List<T> collection, Predicate<T> predicate) {
       List<T> newCollection=new ArrayList<T>();
       if ((collection != null) && (predicate != null)) {
           Iterator<T> itr = collection.iterator();
              while(itr.hasNext()) {
                T obj = itr.next();
                if (predicate.test(obj)) {
                   newCollection.add(obj);
                }
            }
        }
       return newCollection;
    }
}
